package leetcode;

import java.util.Stack;

public class LC20ValidParentheses {

    public boolean isValid(String s) {
        char[] chars =  s.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(' || chars[i] == '{' || chars[i] == '[') {
                stack.push(chars[i]);
            } else {
                if (stack.isEmpty()) return false;
                char c = stack.pop();
                if (chars[i] == ')' && c != '(') return false;
                if (chars[i] == '}' && c != '{') return false;
                if (chars[i] == ']' && c != '[') return false;
            }
        }
        if (!stack.isEmpty()) return false;
        return true;
    }

}
